package net.csdn.business.mirror.config;

import net.csdn.business.mirror.common.Constants;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.listener.PatternTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;

@Configuration
public class RedisListenerConfig {
    @Bean
    @SuppressWarnings({"all"})
    RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory, MessageListenerAdapter listenerMsgAdapter, MessageListenerAdapter listenerNameAdapter) {

        RedisMessageListenerContainer container = new RedisMessageListenerContainer();
        container.setConnectionFactory(connectionFactory);
        // 可以添加多个 messageListener，配置不同的交换机
        container.addMessageListener(listenerMsgAdapter, new PatternTopic(Constants.LOCAL_CACHE_UPDATE_CHANNEL));
        return container;
    }

    /**
     * 绑定消息监听者和接收监听的方法,可配置多个
     */
    @Bean
    @SuppressWarnings({"all"})
    MessageListenerAdapter listenerMsgAdapter(RedisMessageReceiver messageReceiver) {
        return new MessageListenerAdapter(messageReceiver, "onReceiveMessage");
    }

    @Bean
    @SuppressWarnings({"all"})
    StringRedisTemplate template(RedisConnectionFactory connectionFactory) {
        return new StringRedisTemplate(connectionFactory);
    }
}
